/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threads;

import filters.ByteStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import own_packets.Protocol;

/**
 *
 * @author Hnmtuan
 */
public class SearchConnection implements Runnable {

    private String _host;
    private Integer _port;
    private Socket _ownSocket;
    private InputStream _in;
    private OutputStream _output;
    private ObjectOutputStream _objOutput;
    private String _search;
    private File _myFile;
    private String _urlFile;
    private String _fileName;
    private String _downloadHost;
    private Integer _downloadPort;

    public String getDownloadHost() {
        return _downloadHost;
    }

    public void setDownloadHost(String _downloadHost) {
        this._downloadHost = _downloadHost;
    }

    public Integer getDownloadPort() {
        return _downloadPort;
    }

    public void setDownloadPort(Integer _downloadPort) {
        this._downloadPort = _downloadPort;
    }

    public String getFileName() {
        return _fileName;
    }

    public void setFileName(String _fileName) {
        this._fileName = _fileName;
    }
    
    public String getUrlFile() {
        return _urlFile;
    }

    public void setUrlFile(String _urlFile) {
        this._urlFile = _urlFile;
    }

    public File getMyFile() {
        return _myFile;
    }

    public void setMyFile(File _myFile) {
        this._myFile = _myFile;
    }

    public String getSearch() {
        return _search;
    }

    public void setSearch(String _search) {
        this._search = _search;
    }

    public SearchConnection(String host, Integer port) {
        _host = host;
        _port = port;
    }

    public Protocol SendSearchMessages(String search) throws IOException, ClassNotFoundException {
        Protocol obj = null;
        Protocol packet = null;
        // If host and port not configure
        if (_host == null || _port == null) {
            return obj;
        }
        // If not connection success
        if (_in == null && _output == null) {
            return obj;
        }
        // send messages
        packet = new Protocol();
        packet.setIsRequest(Boolean.TRUE);
        packet.setRequestType(Protocol.RequestType.SEARCH_REQUEST);
        packet.setMessages(search);
        // forward messages
        ByteStream.toStream(_output, packet);

        // received messages from server
        obj = ByteStream.toProtocol(_in);

        return obj;
    }

    public void run() {
        try {

            // Connection to server with hostname, portname
            _ownSocket = new Socket(_host, _port);

            // Create outputStream to send data to a following server
            _output = _ownSocket.getOutputStream();

            // Send request type as test connection to a following server
            if (_output == null) {
                System.out.println("Connect to a following server is failed");
                return;
            }

            // Create packet to send a following server
            Protocol myPacket = new Protocol();
            myPacket.setIsRequest(Boolean.TRUE);
            myPacket.setRequestType(Protocol.RequestType.SEARCH_REQUEST);
            myPacket.setMessages((_search != "") ? _search : "no query");

            // Forward packet to a following server
            ByteStream.toStream(_output, myPacket);

            // Create inputStream to received data from a following server
            _in = _ownSocket.getInputStream();

            // Received Response from a following server
            Protocol objRS = ByteStream.toProtocol(_in);

            if (objRS == null) {
                System.out.println("Received Null Object From Server");
            } else {
                // Output result to layout
                System.out.println(String.format("Messages from server : %s", objRS.getMessages()));
                List<pojo.Files> files = objRS.getFiles();

                if (files == null) {
                    System.out.println("Files Search empty");
                } else {
                    for(pojo.Files item : files){
                        _fileName =  item.getFname();
                        _urlFile = item.getFdescription();
                        _downloadHost = item.getPeer();
                        _downloadPort = Integer.parseInt(item.getPeersIdPer());
                        System.out.println(String.format("File URL : %s", _urlFile));
                        System.out.println(String.format("File Name : %s", _fileName));
                    }
                }
            }
        } catch (ClassNotFoundException ex) {
            System.out.println(String.format("Failed TestConnection Thread : %s", ex.getMessage()));
        } catch (UnknownHostException ex) {
            System.out.println(String.format("Failed TestConnection Thread : %s", ex.getMessage()));
        } catch (IOException ex) {
            System.out.println(String.format("Failed TestConnection Thread : %s", ex.getMessage()));
        } finally {
            try {
                _ownSocket.close();
            } catch (IOException ex) {
                System.out.println(String.format("Failed TestConnection Thread : %s", ex.getMessage()));
            }
        }
    }
}
